package com.yunxi.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.util.StringUtils;

/**
 * Created by Administrator on 2019/5/22.
 */
public class DateUtil {
//    public static void main(String[] args) {
//
//        String date1=getBeginTime();
//        String date2=getEndTime();
//        System.out.println(date1+"..."+date2);
//    }

    public static String getBeginTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        String date=sdf.format(calendar.getTime());
        //System.out.println(date);
        //System.out.println(calendar.getTime().toLocaleString());
        return date;
    }
    public static String getEndTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        String date=sdf.format(calendar.getTime());
        //System.out.println(date);
        //System.out.println(calendar.getTime().toLocaleString());
        return date;
    }

    public static String getUtcTime(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = df.parse(date);
        date=sdf.format(parse);
        return date;
    }


    /**
     * Date format
     *
     * @param date date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String format(Date date) {
        return format(date, null);
    }

    /**
     * Date to String
     *
     * @param date date
     * @param pattern default yyyy-MM-dd HH:mm:ss
     * @return the formatted time string.
     */
    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        if (StringUtils.isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * String TO Date
     *
     * @param dateStr dateStr
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static Date parse(String dateStr) {
        return parse(dateStr, null);
    }

    /**
     * String TO Date
     *
     * @param dateStr dateStr
     * @param pattern default yyyy-MM-dd HH:mm:ss
     * @return the formatted time string.
     */
    public static Date parse(String dateStr, String pattern) {
        if (StringUtils.isEmpty(dateStr)) {
            return null;
        }
        if (StringUtils.isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据年月获取一个日期的集合
     *
     * @param year 年
     * @param month 月
     * @return List<Integer>
     */
    public static List<Integer> getDayListByMonth(Integer year, Integer month) {
        List<Integer> days = new ArrayList<>();
        int maxCount = LocalDate.now().withYear(year).withMonth(month).lengthOfMonth();
        for (int i = 1; i <= maxCount; i++) {
            days.add(i);
        }
        return days;
    }

    /**
     * 获取两个时间相差天数
     * @param begin 开始
     * @param end 结束
     * @return 相差天数
     */
    public static Integer getDateDiffByDay(Date begin, Date end) {
        if (begin == null || end == null) {
            return null;
        }
        LocalDate beginDate = begin.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endDate = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return (int) (endDate.toEpochDay() - beginDate.toEpochDay());
    }
}
